"""
@Project ：PythonInterfaceFrame 
@File    ：AllureLogUtil.py
@IDE     ：PyCharm 
@Author  ：ceiling
@Date    ：2025/5/2 18:44 
"""
import json
import allure


class AllureLogUtil:

    @staticmethod
    def attach_request_response(request, response):
        # ---------- 请求信息 ----------
        request_headers = dict(request.headers)

        # 尝试格式化请求体为 JSON
        try:
            request_body_json = json.loads(request.body) if request.body else None
            request_body_formatted = json.dumps(request_body_json, indent=2, ensure_ascii=False)
            body_attachment_type = allure.attachment_type.JSON
        except Exception:
            request_body_formatted = request.body or "No Body"
            body_attachment_type = allure.attachment_type.TEXT

        # 附加请求信息
        allure.attach(json.dumps({
            "URL": request.url,
            "Method": request.method,
            "Headers": request_headers
        }, indent=2, ensure_ascii=False),
            name="Request Info",
            attachment_type=allure.attachment_type.JSON)

        allure.attach(request_body_formatted,
                      name="Request Body",
                      attachment_type=body_attachment_type)

        # ---------- 响应信息 ----------
        response_headers = dict(response.headers)
        content_type = response.headers.get("Content-Type", "")
        response_time = response.elapsed.total_seconds()
        response_size = len(response.content)

        # 附加响应头信息
        allure.attach(json.dumps({
            "Status Code": response.status_code,
            "Response Time (s)": response_time,
            "Response Size (bytes)": response_size,
            "Headers": response_headers
        }, indent=2, ensure_ascii=False),
            name="Response Info",
            attachment_type=allure.attachment_type.JSON)

        # 根据 Content-Type 判断响应体类型
        if "application/json" in content_type:
            try:
                response_json = response.json()
                body = json.dumps(response_json, indent=2, ensure_ascii=False)
                attachment_type = allure.attachment_type.JSON
            except Exception:
                body = response.text
                attachment_type = allure.attachment_type.TEXT

        elif "image/png" in content_type:
            body = response.content
            attachment_type = allure.attachment_type.PNG
        elif "image/jpeg" in content_type:
            body = response.content
            attachment_type = allure.attachment_type.JPG
        elif "application/pdf" in content_type:
            body = response.content
            attachment_type = allure.attachment_type.PDF
        elif "text/html" in content_type:
            body = response.text
            attachment_type = allure.attachment_type.HTML
        else:
            # 其他未知类型作为纯文本处理
            try:
                body = response.text
            except Exception:
                body = response.content
            attachment_type = allure.attachment_type.TEXT

        allure.attach(body, name="Response Body", attachment_type=attachment_type)
